package com.weaver.seconddev.api.tl.contract_manage_api;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import com.weaver.seconddev.api.tl.service.DictCommonService;

/**
 * 合同需求公共方法
 *
 * @author th
 * @date 2023/7/31 9:18
 */
@org.springframework.stereotype.Service("tl_contract_manage_api_ContractManageCommon2")
@Slf4j
public class ContractManageCommon {

    /**
     * 计算下一审批节点
     *
     * @param calculateContractNextNodeReq
     * @return
     */
    public String calculationNextApprovalNode(CalculateContractNextNodeReq calculateContractNextNodeReq) {
        log.info(String.valueOf("======Into NBS Contract Manage Common To Calculation Next Approval Node======"));
        // 筛选需要审批的node
        Map<Long, String> filterNodeIdMap = new HashMap<>();
        List<Long> orderNoList = new ArrayList<>();
        Long currentNodeOrderNo = null;
        for (ContractApprovalHierarchyInfo contractApprovalHierarchyInfo : calculateContractNextNodeReq.getApprovalHierarchyInfoList()) {
            Long orderNo = contractApprovalHierarchyInfo.getOrderNo();
            String nodeId = contractApprovalHierarchyInfo.getNodeId();
            // 代码计算结果
            String selectedStatus = contractApprovalHierarchyInfo.getSelectedStatus();
            // 前端勾选结果
            String frontSelectedStatus = contractApprovalHierarchyInfo.getFrontSelectedStatus();
            // 获取当前节点对应序号
            if (nodeId.equals(calculateContractNextNodeReq.getCurrentNodeId())) {
                currentNodeOrderNo = orderNo;
            }
            // 1、前端勾选为true
            // 2、前端勾选为空，但代码计算结果为true
            if ((StringUtils.isNotBlank(frontSelectedStatus) && "true".equals(frontSelectedStatus)) || (StringUtils.isBlank(frontSelectedStatus) && StringUtils.isNotBlank(selectedStatus) && "true".equals(selectedStatus))) {
                orderNoList.add((Long) orderNo);
                filterNodeIdMap.put((Long) orderNo, nodeId);
            }
        }
        String nextApprovalNodeId = null;
        // 计算下一节点
        if (currentNodeOrderNo != null && !orderNoList.isEmpty()) {
            Long finalCurrentNodeOrderNo = currentNodeOrderNo;
            // 如果当前节点是审批层级中的最高审批节点，读取配置表中归档节点ID返回
            if (finalCurrentNodeOrderNo.equals(Collections.max(orderNoList))) {
                // 获取采购字典配置
                Map<String, Map<String, String>> dictInfoByType = DictCommonService.getDictInfoByType("nbs_contract");
                Map<String, String> modContractDictMap = DictCommonService.get(dictInfoByType.get("nbs_contract"));
                nextApprovalNodeId = modContractDictMap.get("archive_robot_node_id");
            } else {
                Optional<Long> nextValue = orderNoList.stream().filter(orderNo -> orderNo > finalCurrentNodeOrderNo).min(Long::compareTo);
                // 使用 Stream API 获取最小的 value 值
                Optional<Long> minValue = filterNodeIdMap.keySet().stream().min(Comparator.naturalOrder());
                // 如果存在最小值，则返回，否则返回 null 或者自定义默认值
                long minNodeNo = minValue.orElse(null);
                log.info(String.valueOf("minNodeNo:" + minNodeNo));
                long nextNodeNo = nextValue.orElse(Long.parseLong(filterNodeIdMap.get((int) minNodeNo)));
                nextApprovalNodeId = filterNodeIdMap.get((int) nextNodeNo);
            }
        }
        log.info(String.valueOf("======Out CSE Contract Manage Common To Calculation Next Approval Node======nextApprovalNodeId:" + nextApprovalNodeId));
        return nextApprovalNodeId;
    }
}
